5.6 How to partially update - AJAX

  1. Motivations
    • How can you update just a part of a web page? Do you have to redisplay the web page?
    • Can we try Google? Google suggests some good queries without reloading the page.
    • In TRUQA, when the search result needs to be displayed, should MainPage be reloaded?
    • Why not to use iframe that we studied in the previous section?
  2. Let's study AJAX.
    • Read all in AJAX Introduction.
      • Can you explain how AJAX works?
      • What technologies are used together with AJAX?
    • Read and try all in AJAX Examples, especially 'Retrieve the content of a PHP file'.
    • Read and try all in AJAX - Create an XMLHttpRequest Object.
      • How do you create an object in JavaScript?
      • Which object is used for AJAX?
      • How do you check if your browser supports AJAX?
      • Trial 1: Let's check if your browser supports AJAX.


      • Same-origin policy - What does "For security reasons, modern browsers do not allow access across domains." mean?
    • Read and try all in AJAX - Send a Request To a Server.
      • Which method is used to open a channel to a server program?
      • Which method is used to send a message to the server program?
      • How to send GET data?
      • How to send POST data?    xhttp.open("POST", url); xhttp.send("username=John&password=secretepassword");
      • How to avoid using a cached data?    The post method
      • When does 'readystatechange' event occur?
      • How to know if there is a response message set from the server program?
      • Trial 2: Let's to send a query information to TRUQA controller, using the GET method.


      • Trial 3: Let's to send a query information to TRUQA controller, using the POST method.


      • What if you try to retrieve data from another server?
    • Read and try all in AJAX - Server Response.
      • When is this event triggered?
      • What does the value 4 for readyState mean?
      • What does the value 200 for status mean?
      • What is a callback function?
      • Read the last example carefully.
      • List the two types of server response.
      • How to use a text response?
      • How to use an XML response?
    • Read and try all in AJAX - PHP Example.
      • Read the first example carefully. 'Google Suggest' uses the same idea.
      • Can you write your own 'TRU Suggest'? Do you think you will need to send only the new character or the whole string?
      • Do you have to use only PHP for server-side programs?
      • $_REQUEST ???    = $_POST + $_GET + $_COOKIE
      • strtolower(), strlen(), substr(), stristr(), strstr()
    • Read and try all in AJAX - Database Example.
      • Read the first example carefully.
      • Trial 4: Let's to send the 'ListQuestions' command to TRUQA controller, using the POST method. After you complete the test, modify the code so that the GET method can be used.


      • Can you write the table in the client, not the server-side? Then how to send data to the client from the server?
    • XMLHttpRequest objects and addEventListener() - Read Using XMLHttpRequest. XMLHttpRequest objects have different events, such as load and error.
      • Events on an XMLHttpRequest object - ..., 'load' and 'error'
      • Trial 4.5 The same as Trial 4, except events. Let's use the load event.


      • Can you write the table in the client, not the server-side? Then how to send data to the client from the server?
  3. jQuery AJAX functions
    • Read all in AJAX Introduction.
    • Read all in AJAX load().
      • Trial 5: Let's use .load() to display a table sent from TRUQA. (Note that the 'GET' method is assumed when there is no 2nd argument. When the 2nd argument, i.e., query data, is an object, the 'POST' method is used.)


    • Read all in AJAX get() and post().
      • Trial 6: Let's use .post() to display a table sent from TRUQA. After you complete the test, modify the code so that the GET method can be used.


    • Read all in jQuery ajax() Method.
    • How to update '#result-pane' in MainPage of TRUQA, using load()?
      var command = {
          page: 'MainPage',
          command: 'SearchQuestions',
          term: search_terms
      };
      $('#button-search-questions').click(function() {
          var search_terms = ...;
          $('#result-pane').load('controller.php', command);  // when the second arg is object, POST is used. Otherwise, GET is used.
      });
      
    • How to update '#result-pane' in MainPage of TRUQA, using POST?
      $('#button-search-questions').click(function() {
          var search_terms = ...;
          $.post('controller.php',
              {
                  ????
              },
              function(result) {
                  ????
              }
          );
      });
      
    • How to update '#result-pane' in MainPage of TRUQA, using ajax()?
      $('#button-search-questions').click(function() {
          var search_terms = ...;
          $.ajax({
              url: 'controller.php',
              type: 'POST',
              data: {
                  ????
              },
              success: function(result) {
                  ????
              }
          });
      });
      
    • Can we send a form using jQuery AJAX?
      • FormData can be sent with $.ajax(). For example, $.ajax({..., data: new FormData(formDomObject), processData: false, ...});.
      • Trial 7: Let's to send a form for the 'ListQuestions' command to TRUQA controller, using the jQuery ajax() method.


  4. Is there any formal format of data that is sent from the server?
    • It will be discussed in the next section.

  5. Some review questions and learning outcomes
    • Use of AJAX
    • Use of jQuery AJAX functions